﻿Public Class Token

	Dim Connection As New Database
	Dim Window As Window

	Sub New(ByVal Window As Window)
		Me.Window = Window
	End Sub

	<DebuggerHidden()> _
	Public Sub Open(ByVal ConnectionString As String, Optional ByVal Tick As Recordset.Tick = Nothing)
		Connection.Create(ConnectionString, Tick)
	End Sub

	ReadOnly Property Process As Process
		Get
			Return CType(Window.ActivePage, Process)
		End Get
	End Property

	<DebuggerHidden()>
	Friend Function OpenSQL(ByVal Text As String, ByVal ParamArray Args() As Object) As Recordset
		If Text = "*" Then
			Text = SQLBuffer
			SQLBuffer = ""
		End If
		Dim Rs As Recordset
		Process.UseWaitCursor = True
		Process.TopPanel.StartProgress()
		Try
			Rs = Connection.OpenSQL(AddressOf Process.TopPanel.Tick, Text, Args)
			Process.TopPanel.StopProgress()
			Process.UseWaitCursor = False
			Return Rs
		Catch ex As Exception
			Process.TopPanel.StopProgress()
			Process.UseWaitCursor = False
			Err.Raise(vbObjectError + 513, "SQL", ex.Message)
			Return Nothing
		End Try
	End Function

	<DebuggerHidden()>
	Friend Sub ExecSQL(ByVal Text As String, ByVal ParamArray Args() As Object)
		Process.UseWaitCursor = True
		If Text = "*" Then
			Text = SQLBuffer
			SQLBuffer = ""
		End If
		Process.UseWaitCursor = True
		Process.TopPanel.StartProgress()
		Try
			Connection.ExecSQL(AddressOf Process.TopPanel.Tick, Text, Args)
			Process.TopPanel.StopProgress()
			Process.UseWaitCursor = False
		Catch ex As Exception
			Process.TopPanel.StopProgress()
			Process.UseWaitCursor = False
			Err.Raise(vbObjectError + 513, "SQL", ex.Message)
		End Try
	End Sub

	<DebuggerHidden()> _
	Friend Sub BeginTran()
		Connection.BeginTran()
		'ExecSQL("begin tran")
	End Sub

	<DebuggerHidden()> _
	Friend Sub CommitTran()
		Connection.CommitTran()
		'ExecSQL("commit tran")
	End Sub

	<DebuggerHidden()> _
	Friend Sub Rollback(Optional ByVal ErrorMsg As String = "")
		Try
			ExecSQL("rollback tran")
		Catch ex As Exception
		End Try
		Warning("Kayıt işlemi sırasında bir hata oluştu.", ErrorMsg)
	End Sub

	ReadOnly Property SysDate() As Date
		<DebuggerHidden()> _
		Get
			Return OpenSQL("select convert(smalldatetime,convert(char(8),getdate(),112))")(0)
		End Get
	End Property

	ReadOnly Property SysTime() As Date
		<DebuggerHidden()> _
		Get
			Return OpenSQL("select getdate()")(0)
		End Get
	End Property

	<DebuggerHidden()> _
	Friend Sub Save(ByVal Table As String, ByVal Fields As String, ByVal ParamArray Values() As Object)
		If InStr(Table, "(") = 0 Then Table &= "(1=0)"
		Dim Pos As Integer = InStr(Table, "(")
		Dim Rs As Recordset = OpenSQL("select * from " & Mid(Table, 1, Pos - 1) & " where " & Mid(Table, Pos))
		If Rs.EOF Then Rs.AddNew()
		Rs.SetValues(Fields, Values)
		Rs.Save()
	End Sub

	<DebuggerHidden()> _
	Friend Function OpenTable(ByVal Text As String, ByVal ParamArray Args() As Object) As Recordset
		Dim Items() As String = Text.Split("(")
		Dim i As Integer = 0
		Items(1) = Items(1).Replace(")", "")
		Text = "select * from " & Items(0) & " where 1 = 1"
		If Items(1) = "new" Then
			Text &= " and 1 = 0"
		Else
			For Each Field As String In Items(1).Split(",")
				Text &= " and " & Field & " = '" & Args(i) & "'"
				i += 1
			Next
		End If
		Return Connection.OpenSQL(AddressOf Process.TopPanel.Tick, Text)
	End Function

	<DebuggerHidden()> _
	Friend Sub DropTemporaryTables(ByVal Tables As String)
		For Each Table As String In Tables.Split(",")
			ExecSQL("if object_id('tempdb..#" & Table & "') is not null drop table #" & Table)
		Next
	End Sub

End Class
